﻿#include "DrawColumn.h"

#include <QPainter>

DrawColumn::DrawColumn(QWidget *parent)
    :QWidget(parent)
{

}

DrawColumn::~DrawColumn()
{

}

void DrawColumn::SetNum(int n)
{
    m_iNum = n;
    update();
}

void DrawColumn::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter painter(this);
    int nWidth = this->width();
    int nHeight = this->height();
    painter.fillRect(0, 0, nWidth, nHeight, QColor(0, 0, 0));
    painter.save();
    QPen pen(QColor(0, 150, 0), 3.0);
    painter.setPen(pen);

    int nDrawWidth = (nWidth - 30)/2;
    int nDrawHeight = (nHeight - 30)/10;

    int nY = nHeight / 2 - nDrawHeight + 5 * nDrawHeight;

    int nlx1 = nWidth / 2 - nDrawWidth - 3, nlx2 = nWidth / 2 - 3;
    int nrx1 = nWidth / 2 + 3, nrx2 = nWidth / 2 + nDrawWidth + 3;
    for (int i = 0; i < 10; i++)
    {
        painter.drawLine(nlx1, nY - nDrawHeight*i, nlx2, nY-nDrawHeight*i);
        painter.drawLine(nrx1, nY - nDrawHeight*i, nrx2, nY-nDrawHeight*i);
    }
    painter.restore();
    painter.setPen(QPen(Qt::green, 3.0));
    painter.setFont(QFont("SimSun",16));
    QString snum = QString::number(m_iNum) + "%";
    painter.drawText(nWidth / 2 - snum.size()*10 / 2, nHeight-nDrawHeight/2, snum);
    int count = m_iNum / 10;
    for (int i = 0; i < count; i++)
    {
        painter.drawLine(nlx1, nY - nDrawHeight*i, nlx2, nY - nDrawHeight*i);
        painter.drawLine(nrx1, nY - nDrawHeight*i, nrx2, nY - nDrawHeight*i);
    }
}
